package com.ih.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ih.pojo.Customer;
import com.ih.pojo.MainMenu;
import com.ih.pojo.Worker;
import com.ih.service.WorkerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/worker")
@Slf4j
public class WorkerController {

    @Autowired
    private WorkerService workerService;

    @Value("${location}")  //获取yml 中的文件路径
    private String location;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询所有送水工信息
     *
     * @param model
     * @return
     */
    /*@RequestMapping("/workerList")
    public String listWorker(Model model) {
        List<Worker> workerList = workerService.queryWorker();
        model.addAttribute("workerList", workerList);
        return "workerList";
    }*/
    /**
     * 模糊查询带分页
     * @param pageNum
     * @param pageSize
     * @param worker
     * @param model
     * @return
     */
    @RequestMapping("/workerList")
    public String listCust(@RequestParam(required = false,defaultValue = "1",value = "pageNum") Integer pageNum,
                           @RequestParam(required = false,defaultValue = "10",value = "pageSize") Integer pageSize, Worker worker, Model model) {
        if (pageNum==null||pageNum<=0 || pageNum.equals("")){
            pageNum=1;
        }
        if (pageSize==null||pageSize<=0 || pageSize.equals("")){
            pageSize=10;
        }
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Worker> pageInfo = workerService.listWorker(worker, pageNum, pageNum);
        model.addAttribute("pageInfo", pageInfo);
        return "workerList";
    }

    /**
     * 添加送水工信息页面跳转
     *
     * @return
     */
    @RequestMapping("preSaveWorker")
    public String saveWorker() {
        return "workerSave";
    }

    /**
     * 添加送水工信息
     *
     * @param worker
     * @param file
     * @return
     */
    @PostMapping(value = "workerSave")
    public String workerSave(Worker worker, MultipartFile file) {
        //添加信息  图片上传 姓名添加到redis

        if (file.getOriginalFilename().lastIndexOf(".")!=-1){
            transerFile(worker, file);
        }
        workerService.saveWorker(worker);
        //redis
        // redisTemplate.delete("mainMenu");
        Long size = redisTemplate.opsForList().size("mainMenu");
        if (size < 12) {
            MainMenu mainMenu = new MainMenu();
            mainMenu.setWorkerName(worker.getWorkerName());
            mainMenu.setSendWaterCount(0);
            redisTemplate.opsForList().rightPush("mainMenu", mainMenu);
        }
        return "redirect:/worker/workerList";
    }

    /**
     * 图片上传
     *
     * @param worker
     * @param file
     */
    private void transerFile(Worker worker, MultipartFile file) {
        if (file != null) {
            //获得上传文件的名称
            String filename = file.getOriginalFilename();
            //获取文件后缀索引
            int index = filename.lastIndexOf(".");
            //获取后缀  如：.png
            String suffix = filename.substring(index);
            //前缀
            long prefix = System.currentTimeMillis();
            //拼接
            String uploadFileName = prefix + suffix;
            //创建服务器存放浏览器上传文件的file对象
            File uploadFile = new File(location);
            //判断路径是否存在
            if (!uploadFile.exists()) {
                uploadFile.mkdirs();
            }
            //创建上传文件的路径  参数一 文件路径  参数二 文件名称
            File path = new File(uploadFile, uploadFileName);
            try {
                file.transferTo(path);
            } catch (IOException e) {
                e.printStackTrace();
            }

            worker.setWorkerImage(uploadFileName);
        }
    }

    /**
     * 修改送水工信息页面跳转
     *
     * @return
     */
    @RequestMapping("/preUpdateWorker/{wid}")
    public String updateWorker(@PathVariable("wid") Integer wid, Model model) {
        Worker worker = workerService.getWorkerById(wid);
        model.addAttribute("worker", worker);
        return "workerUpdate";
    }

    /**
     * 修改送水工信息
     * @param worker
     * @param file
     * @return
     */
    @PostMapping("/updateWorker")
    public String updateWorker(Worker worker, MultipartFile file) {
        System.out.println("file = " + file.getOriginalFilename());
        if (!file.getOriginalFilename().equals("")){
            transerFile(worker,file);
        }
        workerService.updateWorker(worker);
        return "redirect:/worker/workerList";
    }

    /**
     * 工资调整
     * @param wid
     * @param workerSalary
     * @return
     */
    @PostMapping("/addSalary")
    @ResponseBody
    public String addSalary(Integer wid,Integer workerSalary){
        int row = workerService.addSalary(wid, workerSalary);
        if (row>0){
            return "ok";
        }else {
            return "error";
        }


    }

}
